package top.idcard.agent.biz.entity;

import com.alibaba.fastjson.JSON;
import top.idcard.agent.biz.util.ByteUtil;

import java.nio.ByteBuffer;

/**
 * @author wx
 * @version 1.0
 * @date 2021/1/29 5:23 下午
 * 大陆居民身份证实体类
 */
public class NativeIDCard extends Certification{

    //姓名  30
    private String name;
    //性别  2
    private Short sex;
    //民族  20
    private String nation;
    //出生日期  16
    private String birthday;
    //住址  70
    private String addr;
    //身份证号码  36
    private String no;
    //签发机关  30
    private String signOrganization;
    //有效期起始日期  16
    private String validationStart;
    //有效期结束日期  16
    private String validationEnd;
    //头像  38862
    private byte[] img;
    //指纹  1024
    private byte[] fingerprint;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Short getSex() {
        return sex;
    }

    public void setSex(Short sex) {
        this.sex = sex;
    }

    public String getNation() {
        return nation;
    }

    public void setNation(String nation) {
        this.nation = nation;
    }

    public String getBirthday() {
        return birthday;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }

    public String getAddr() {
        return addr;
    }

    public void setAddr(String addr) {
        this.addr = addr;
    }

    public String getNo() {
        return no;
    }

    public void setNo(String no) {
        this.no = no;
    }

    public String getSignOrganization() {
        return signOrganization;
    }

    public void setSignOrganization(String signOrganization) {
        this.signOrganization = signOrganization;
    }

    public String getValidationStart() {
        return validationStart;
    }

    public void setValidationStart(String validationStart) {
        this.validationStart = validationStart;
    }

    public String getValidationEnd() {
        return validationEnd;
    }

    public void setValidationEnd(String validationEnd) {
        this.validationEnd = validationEnd;
    }

    public byte[] getImg() {
        return img;
    }

    public void setImg(byte[] img) {
        this.img = img;
    }

    public byte[] getFingerprint() {
        return fingerprint;
    }

    public void setFingerprint(byte[] fingerprint) {
        this.fingerprint = fingerprint;
    }

    @Override
    public CertificationEnum certificationType() {
        return CertificationEnum.Native;
    }

    @Override
    public byte[] getBytes() {
        ByteBuffer byteBuffer = ByteBuffer.allocate(40123);
        int offset = 0;

        byteBuffer.put(CertificationEnum.Native.getCertificationValue().getBytes());
        offset += 1;

        byteBuffer.put(ByteUtil.string2Byte(this.getName(), 30), 0, 30);
        offset += 30;

        byteBuffer.put(ByteUtil.short2Byte(this.getSex(), 2));
        offset += 2;

        byteBuffer.put(ByteUtil.string2Byte(this.getNation(), 30), 0, 20);
        offset += 20;

        byteBuffer.put(ByteUtil.string2Byte(this.getBirthday(), 16), 0, 16);
        offset += 16;

        byteBuffer.put(ByteUtil.string2Byte(this.getAddr(), 70), 0, 70);
        offset += 70;

        byteBuffer.put(ByteUtil.string2Byte(this.getNo(), 36), 0, 36);
        offset += 36;

        byteBuffer.put(ByteUtil.string2Byte(this.getSignOrganization(), 30), 0, 30);
        offset += 30;

        byteBuffer.put(ByteUtil.string2Byte(this.getValidationStart(), 16), 0, 16);
        offset += 16;

        byteBuffer.put(ByteUtil.string2Byte(this.getValidationEnd(), 16), 0, 16);
        offset += 16;

        byteBuffer.put(ByteUtil.img2Byte(this.getImg()), 0, 38862);
        offset += 38862;

        byteBuffer.put(ByteUtil.Fingerprint2Byte(this.getFingerprint()), 0, 1024);
        offset += 1024;
        return byteBuffer.array();
    }

    @Override
    public NativeIDCard parseFrom(byte[] bytes){
        String str = new String(bytes);
        return JSON.parseObject(str, NativeIDCard.class);
    }
}
